﻿#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdio.h>
//int main()
//{
//	FILE* pFile;
//	//打开⽂件
//	pFile = fopen("test.txt", "r");
//	//⽂件操作
//	if (pFile == NULL)
//	{
//		perror("fopen");
//		return 1;
//	}
//	//char ch = 0;
//	///*fputc('b', pFile);
//	//int a = fputc('b', pFile);
//	//printf("%d", a);*/
//	//for ( ch ='a';ch <='z'; ch++)
//	//{
//	//	fputc(ch, pFile);
//	//	int a = fputc('ch', pFile);
//	//	printf("%d", a);
//	//}
//	//读文件
//
//	/*int ch = fgetc(pFile);
//	printf("%c\n", ch);
//     ch = fgetc(pFile);
//	 printf("%c\n", ch);
//	 ch = fgetc(pFile);
//	 printf("%c\n", ch); 
//	 ch = fgetc(pFile);
//	 printf("%c\n", ch);*/
//	int ch = 0;
//	while ((ch = fgetc(pFile)) != EOF)
//	{
//		printf("%c", ch);
//	}
//
//	//关闭⽂件
//	fclose(pFile);
//	pFile = NULL;
//	return 0;
//}

int main()
{
	FILE* pFile;
	//打开⽂件
	pFile = fopen("test.txt", "r");
	//⽂件操作
	if (pFile == NULL)
	{
		perror("fopen");
		return 1;
	}
	char arr[20] = { 0 };
	char* pf;
	while (( pf =fgets(arr, 20, pFile))!= NULL)
	{
		printf("%s", arr);
	}
    close(pFile);
     pFile = NULL;
	return 0;
}

struct s
{
	char name[20];
	int age;
	float score;
};


int main()
{
	struct s s = { "张三",20,65.5f};
	//打开⽂件
	FILE* pf = fopen("test.txt", "w");
	//⽂件操作
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fprintf(pf, "%s %d %f", s.name, s.age, s.score);
	fclose(pf);
	pf = NULL;
	return 0;
}
int main()
{
	struct s s = {0};
	//打开⽂件
	FILE* pf = fopen("test.txt", "r");
	//⽂件操作
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.score));
	printf( "%s %d %f\n", s.name, s.age, s.score);
	fclose(pf);
	pf = NULL;
	return 0;
}


struct s
{
	char name[20];
	int age;
	float score;
};
int main()
{
	char buf[200] = { 0 };
	struct s s = { "张三",20,65.5f };
	sprintf(buf, "%s %d %f", s.name, s.age, s.score);


	printf("1以字符串的形式:%s\n", buf);
	struct s t = { 0 };
	sscanf(buf, "%s %d %f", t.name, &(t.age), &(t.score));
	printf("2按照格式打印： %s %d %f\n", t.name, t.age, t.score);


	return 0;
}



//以二进制读取
int main()
{
	int arr[5] = { 0 };
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fread(arr, sizeof(arr[0]), 5, pf);
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr[i]);
	}

	fclose(pf);
	pf = NULL;

	return 0;
}


int main()
{
	int arr[5] = { 0 };
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int i = 0;
	
	while (fread(arr+i, sizeof(arr[0]), 1, pf))
	{
		printf("%d ", arr[i]);
		i++;
	}
	fclose(pf);
	pf = NULL;

	return 0;
}

int main()
{
	int arr[5] = { 1,2,3,4,5 };
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int sz = sizeof(arr) / sizeof(arr[0]);
	fwrite(arr, sizeof(arr[0]), sz, pf);//以二进制的形式写进去的
	fclose(pf);
	pf = NULL;

	return 0;
}


//abcdef
int main()
{
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int ch = fgetc(pf);
	printf("%c\n", ch);//a
	fseek(pf, 4, SEEK_CUR);
	ch = fgetc(pf);
	printf("%c\n", ch);//f

	fclose(pf);
	pf = NULL;

	return 0;
}


int main()
{
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int ch = fgetc(pf);
	printf("%c\n", ch);//a
	fseek(pf, -4, SEEK_END);
	printf("%d\n",ftell(pf));//a

	fclose(pf);
	pf = NULL;

	return 0;
}



int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int ch = 0;
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c\n", ch);
	}
	if (feof(pf))
	{
		printf("遇到文件末尾，读取正常结束\n");
	}
	else if (ferror(pf))
	{
		printf("fgetc");
	}
	fclose(pf);
	pf = NULL;

	return 0;
}


//⽂件读取结束的判定
//⽂本⽂件的例⼦：
int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写
	char ch = 0;
	for (ch = 'a'; ch <= 'z'; ch++)
	{
		fputc(ch, pf);
	}
	
	if (feof(pf))
	{
		printf("遇到文件末尾，读取正常结束\n");
	}
	else if (ferror(pf))
	{
		printf("fgetc");
	}
	fclose(pf);
	pf = NULL;

	return 0;
}

//⼆进制⽂件的例⼦：
enum { SIZE = 5 };
int main(void)
{
	double a[SIZE] = { 1.,2.,3.,4.,5. };
	FILE* fp = fopen("test.bin", "wb"); // 必须⽤⼆进制模式
	fwrite(a, sizeof * a, SIZE, fp); // 写 double 的数组
	fclose(fp);
	double b[SIZE];
	fp = fopen("test.bin", "rb");
	size_t ret_code = fread(b, sizeof * b, SIZE, fp); // 读 double 的数组
	if (ret_code == SIZE) {
		puts("Array read successfully, contents: ");
		for (int n = 0; n < SIZE; ++n)
			printf("%f ", b[n]);
		putchar('\n');
	}
	else { // error handling
		if (feof(fp))
			printf("Error reading test.bin: unexpected end of file\n");
		else if (ferror(fp)) {
			perror("Error reading test.bin");
		}
	}
	fclose(fp);
}








#include <windows.h>
//VS2022 WIN11环境测试
int main()
{
	FILE* pf = fopen("test.txt", "w");
	fputs("abcdef", pf);//先将代码放在输出缓冲区
	printf("睡眠10秒-已经写数据了，打开test.txt⽂件，发现⽂件没有内容\n");
	Sleep(10000);
	printf("刷新缓冲区\n");
	fflush(pf);//刷新缓冲区时，才将输出缓冲区的数据写到⽂件（磁盘）
	//注：fflush 在⾼版本的VS上不能使⽤了
	printf("再睡眠10秒-此时，再次打开test.txt⽂件，⽂件有内容了\n");
	Sleep(10000);
	fclose(pf);
	//注：fclose在关闭⽂件的时候，也会刷新缓冲区
	pf = NULL;
	return 0;
}